package tinyms.odds;


import org.apache.commons.io.FileUtils;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StoredField;
import org.apache.lucene.document.StringField;
import org.apache.lucene.document.TextField;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.search.highlight.Highlighter;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tornadoj.data.ILuceneSearch;
import tornadoj.data.ILuceneWriter;
import tornadoj.data.LuceneUtil;
import tornadoj.points.IDownload;
import tornadoj.util.Http;
import tornadoj.util.Utils;

import java.io.File;
import java.io.IOException;
import java.util.*;

/**
 * Parse Html to format data for lucene.
 * Created by i@tinyms.com on 2014/11/12.
 */
public class Data {
    private final static Logger logger = LoggerFactory.getLogger(Data.class);
    public final static String companNames = ",90,81,115,517,474,82,499,80,110,545,177,370,104,255,281,16,659,450,825,60,180,";
    public static List<List<String>> expect(String url){
        List<List<String>> rows = new ArrayList<List<String>>();
        Http.download(url, new IDownload() {
            @Override
            public boolean doInDownload(String downloadedFile) {
                try {
                    Document doc = Jsoup.parse(new File(downloadedFile), "utf-8");
                    Iterator<Element> elements = doc.select("table.socai tr").iterator();
                    while (elements.hasNext()){
                        Element tr = elements.next();
                        String cssCls = tr.attr("class");
                        if(!cssCls.contains("ni")){
                            continue;
                        }
                        List<String> row = new ArrayList<String>();
                        Iterator<Element> tds = tr.select("td").iterator();
                        while (tds.hasNext()){
                            Element td = tds.next();
                            row.add(td.text());
                        }
                        Iterator<Element> links = tr.select("a").iterator();
                        while (links.hasNext()){
                            Element link = links.next();
                            if(link.attr("href").contains("/handicap/")){
                                row.add(7, Utils.findNumber(link.attr("href")).get(0));
                            }
                        }
                        rows.add(row.subList(0, 8));
                    }
                } catch (IOException e) {
                    logger.info(e.getLocalizedMessage(), e);
                }
                return true;
            }
        });
        return rows;
    }

    public static void parse(List<List<String>> rows){
        List<String> urls = new ArrayList<String>();
        for(List<String> row : rows){
            urls.add(String.format("http://www.310win.com/handicap/%s.html", row.get(7)));
            urls.add(String.format("http://www.310win.com/info/1x2exchange.aspx?id=%s&cids=%s&type=2", row.get(7), companNames));
        }
        Http.downloads(urls, new IDownload() {
            @Override
            public boolean doInDownload(String downloadedFile) {
                return false;
            }
        });
        for(List<String> row : rows){
            String asia = parseAsia(row.get(7));
            String ou = parseOU(row.get(7));
            row.add(asia);
            row.add(ou);
        }
    }

    public static String parseAsia(String id){
        List<List<String>> rows = new ArrayList<List<String>>();
        String url = String.format("http://www.310win.com/handicap/%s.html", id);
        String targetFile = String.format("%s/%s.dl", Http.cachePath, Utils.md5(url));
        try {
            File f = new File(targetFile);
            if(!f.exists()){
                return "[]";
            }
            Document doc = Jsoup.parse(f, "utf-8");
            Element table = doc.select("table.socai").get(0);
            Iterator<Element> trs = table.select("tr").iterator();
            while (trs.hasNext()){
                Element tr = trs.next();
                if(!tr.attr("class").contains("ni")){
                    continue;
                }
                List<String> row = new ArrayList<String>();
                Iterator<Element> tds = tr.select("td").iterator();
                while (tds.hasNext()){
                    Element td = tds.next();
                    row.add(td.text());
                }
                rows.add(row.subList(0, 4));
            }
        } catch (IOException e) {
            logger.info(e.getLocalizedMessage(), e);
        }
        FileUtils.deleteQuietly(new File(targetFile));
        return Utils.encode(rows);
    }

    public static String parseOU(String id){
        List<List<String>> rows = new ArrayList<List<String>>();
        String url = String.format("http://www.310win.com/info/1x2exchange.aspx?id=%s&cids=%s&type=2", id, companNames);
        String targetFile = String.format("%s/%s.dl", Http.cachePath, Utils.md5(url));
        try {
            File f = new File(targetFile);
            if(!f.exists()){
                return "[]";
            }
            Document doc = Jsoup.parse(f, "utf-8");
            Iterator<Element> trs = doc.select("table.tcenter2 tr").iterator();
            while (trs.hasNext()){
                Element tr = trs.next();
                if(!"#FFFFFF".equalsIgnoreCase(tr.attr("bgcolor"))){
                    continue;
                }
                List<String> row = new ArrayList<String>();
                Iterator<Element> tds = tr.select("td").iterator();
                while (tds.hasNext()){
                    Element td = tds.next();
                    row.add(td.text());
                }
                rows.add(row.subList(0, 4));
            }
        } catch (IOException e) {
            logger.info(e.getLocalizedMessage(), e);
        }
        FileUtils.deleteQuietly(new File(targetFile));
        return Utils.encode(rows);
    }

    public static void main(String[] args){
        List<List<String>> result = expect("http://www.310win.com/info/match/Zucai.aspx?TypeID=1");
        parse(result);
        logger.info(Utils.encode(result));
        LuceneUtil lucene = new LuceneUtil("d:/lucene-odds/");
        lucene.write(new ILuceneWriter() {
            @Override
            public void doInWrite(IndexWriter writer) {
                for(List<String> item : result) {
                    org.apache.lucene.document.Document doc = new org.apache.lucene.document.Document();
                    String text = Utils.encode(item);
                    doc.add(new StoredField("event", item.get(1)));
                    doc.add(new StoredField("time", item.get(2)));
                    doc.add(new StoredField("main", item.get(4)));
                    doc.add(new StoredField("score", item.get(5)));
                    doc.add(new StoredField("client", item.get(6)));
                    doc.add(new StoredField("id", item.get(7)));
                    doc.add(new StoredField("asia", item.get(8)));
                    doc.add(new StoredField("ou", item.get(9)));
                    doc.add(new TextField("indexer", item.get(8), Field.Store.YES));
                    try {
                        writer.addDocument(doc);
                    } catch (IOException e) {
                        logger.info(e.getLocalizedMessage(), e);
                    }
                }
            }
        });
//        Object querys = lucene.paginate("半球", new String[]{"indexer"}, 1, 20, new ILuceneSearch() {
//            @Override
//            public Object doInSearch(org.apache.lucene.document.Document doc, Analyzer analyzer, Highlighter highlighter) {
//                Map<String, String> item = new HashMap<String, String>();
//                String event = LuceneUtil.colorKeyWords(doc, analyzer, highlighter, "event");
//                item.put("event", event);
//                String time = LuceneUtil.colorKeyWords(doc, analyzer, highlighter, "time");
//                item.put("time", time);
//                String main = LuceneUtil.colorKeyWords(doc, analyzer, highlighter, "main");
//                item.put("main", main);
//                String score = LuceneUtil.colorKeyWords(doc, analyzer, highlighter, "score");
//                item.put("score", score);
//                String client = LuceneUtil.colorKeyWords(doc, analyzer, highlighter, "client");
//                item.put("client", client);
//                String id = LuceneUtil.colorKeyWords(doc, analyzer, highlighter, "id");
//                item.put("id", id);
//                String asia = LuceneUtil.getField(doc, "asia");
//                item.put("asia", asia);
//                String ou = LuceneUtil.getField(doc, "ou");
//                item.put("ou", ou);
//                String indexer = LuceneUtil.colorKeyWords(doc, analyzer, highlighter, "indexer");
//                item.put("indexer", indexer);
//                return item;
//            }
//        });
//        logger.info(Utils.encode(querys));
    }
}
